package fm.dongman.modellib.source

import org.json.JSONObject
import java.util.ArrayList
import fm.dongman.animefm.contractlib.BaseDataSource
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.ComicListContract
import fm.dongman.modellib.RemoteHelper
import fm.dongman.modellib.StaticArea.StaticUrl
import fm.dongman.modellib.local.DataOperationHelper
import fm.dongman.modellib.remote.RemoteOperationHelper

/**
 * 番剧列表，观看记录和搜索结果
 * Created by shize on 2017/10/26.
 */

class ComicListRepository : ComicListContract.IComicListDataSource {
    private var mSize: Int = 10 // 每页的数据量
    private var key:String  = "";

    override fun searchComics(key: String, page: Int, callback: BaseDataSource.LoadSourceCallback<List<IModel.IComicModel>>) {
        // http://api.dongman.fm/search/q?q=海贼王&page=1&size=3&type=subject
        val jsonObj = RemoteHelper.instance.parseGetUrl(StaticUrl.COMIC_LIST_REMOTE_KEY + "q=$key&page=$page&size=$mSize&type=subject")
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        this.key = key
        callback.onDataLoaded(downSearchResultData(jsonObj))
    }

    override fun lookedComics(id: String, page: Int, callback: BaseDataSource.LoadSourceCallback<List<IModel.IComicModel>>) {
        val jsonObj = RemoteHelper.instance.parsePostUrl(StaticUrl.COMIC_LIST_REMOTE_ID, mapOf(Pair("session_key", StaticUrl.TOKEN_KEY), Pair("size", mSize), Pair("page", page)))
        if (jsonObj == null) {
            callback.onDataLoadFiled()
            return
        }
        callback.onDataLoaded(downLookedData(jsonObj))
    }

    /**
     * 解析搜索动漫结果列表
     */
    private fun downSearchResultData(jsonObject: JSONObject): List<IModel.IComicModel> {
        val models = ArrayList<IModel.IComicModel>()
        try {
            val jsonArray = jsonObject.getJSONObject("data").getJSONArray("list")
            (0 until jsonArray.length()).forEach {
                models.add(RemoteOperationHelper.getOperationHelper().parseComicModel(jsonArray.getJSONObject(it)))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return models
    }

    /**
     * 解析观看记录列表
     */
    private fun downLookedData(jsonObject: JSONObject): List<IModel.IComicModel> {
        val models = ArrayList<IModel.IComicModel>()
        try {
            val jsonArray = jsonObject.getJSONObject("data").getJSONArray("list")
            (0 until jsonArray.length()).forEach {
                models.add(RemoteOperationHelper.getOperationHelper().parseComicModel(jsonArray.getJSONObject(it)))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return models
    }

    override fun getData(callback: BaseDataSource.LoadSourceCallback<String>) {}

    override fun saveData(dataModel: String) {
        DataOperationHelper.getInstance().saveSearchKey(dataModel)
    }

    override fun refreshData() {}

    override fun offLine() {}

    companion object {
        private var SINGLETON: ComicListRepository? = null

        val instance: ComicListRepository
            get() {
                if (SINGLETON == null) {
                    SINGLETON = ComicListRepository()
                }
                return SINGLETON!!
            }
    }
}
